<template>
  <el-dialog
    v-model="visible"
    :title="title"
    width="600"
    draggable
    @close="close"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :z-index="2000"
  >
    <template #default>
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="80"
        label-position="top"
        @keydown.enter.stop
        @submit.prevent
      >
        <el-form-item label="开发人员审核" prop="reviewer_id">
          <select-personnel
            class="w-full"
            v-model="form.reviewer_id"
            placeholder="添加人"
          ></select-personnel>
        </el-form-item>
        <el-form-item label="">
          <el-checkbox v-model="form.is_notify" label="消息通知审核人员" size="large" />
        </el-form-item>
      </el-form>
    </template>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="save(formRef)"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>
<script setup name="SubmitAudit">
import api from "../api";

import { reactive, toRefs, getCurrentInstance } from "vue";
import { ElMessage } from "element-plus";
const { proxy } = getCurrentInstance();
const emits = defineEmits(["query"]);

const statusMap = new Map([
  ["submitAudit", "提交开发审核"],
  ["operation-audit", "提交运营审核"],
]);

const data = reactive({
  form: {
    productId: [],
    reviewer_id: "",
    is_notify: false,
  },
  visible: false,
  loading: false,
  formRef: null,
  rules: {
    reviewer_id: [{ required: true, message: "请选择开发人员" }],
  },
  title: "",
  handleType: "",
});
const { rules, form, visible, loading, formRef, title, handleType } = toRefs(data);
const open = (type, productId) => {
  title.value = statusMap.get(type);
  handleType.value = type;
  if (productId) form.value.productId = productId;
  visible.value = true;
};
const save = () => {
  if (!formRef.value) return;
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      api
        .submitReview(form.value)
        .then((res) => {
          ElMessage.success(res.message);
          emits("query");
          close();
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
};
const close = () => {
  proxy.resetForm("formRef");
  visible.value = false;
};
defineExpose({ open });
</script>
